﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using YJKPresentation.Utils;

namespace YJKPresentation.CrossFloor
{
	public class ClassificationBatchConverterData
	{
		public List<ElementId> RailingGeneralElementsIdList
		{
			get
			{
				return this.m_RailingGeneralElementsIdList;
			}
			set
			{
				this.m_RailingGeneralElementsIdList = value;
			}
		}

		public List<ElementId> StairsGeneralElementsIdList
		{
			get
			{
				return this.m_StairsGeneralElementsIdList;
			}
			set
			{
				this.m_StairsGeneralElementsIdList = value;
			}
		}

		public List<ElementId> GroupGeneralElementsIdList
		{
			get
			{
				return this.m_GroupGeneralElementsIdList;
			}
			set
			{
				this.m_GroupGeneralElementsIdList = value;
			}
		}

		public List<ElementId> RevitLinkInstanceGeneralElementsIdList
		{
			get
			{
				return this.m_RevitLinkInstanceGeneralElementsIdList;
			}
			set
			{
				this.m_RevitLinkInstanceGeneralElementsIdList = value;
			}
		}

		public List<ElementId> OpeningGeneralElementsIdList
		{
			get
			{
				return this.m_OpeningGeneralElementsIdList;
			}
			set
			{
				this.m_OpeningGeneralElementsIdList = value;
			}
		}

		public List<ElementId> TextNoteGeneralElementsIdList
		{
			get
			{
				return this.m_TextNoteGeneralElementsIdList;
			}
			set
			{
				this.m_TextNoteGeneralElementsIdList = value;
			}
		}

		public List<ElementId> WallGeneralElementsIdList
		{
			get
			{
				return this.m_WallGeneralElementsIdList;
			}
			set
			{
				this.m_WallGeneralElementsIdList = value;
			}
		}

		public List<ElementId> CeilingGeneralElementsIdList
		{
			get
			{
				return this.m_CeilingGeneralElementsIdList;
			}
			set
			{
				this.m_CeilingGeneralElementsIdList = value;
			}
		}

		public List<ElementId> FloorGeneralElementsIdList
		{
			get
			{
				return this.m_FloorGeneralElementsIdList;
			}
			set
			{
				this.m_FloorGeneralElementsIdList = value;
			}
		}

		public List<ElementId> PipeGeneralElementsIdList
		{
			get
			{
				return this.m_PipeGeneralElementsIdList;
			}
			set
			{
				this.m_PipeGeneralElementsIdList = value;
			}
		}

		public List<ElementId> FlexPipeGeneralElementsIdList
		{
			get
			{
				return this.m_FlexPipeGeneralElementsIdList;
			}
			set
			{
				this.m_FlexPipeGeneralElementsIdList = value;
			}
		}

		public List<ElementId> DuctGeneralElementsIdList
		{
			get
			{
				return this.m_DuctGeneralElementsIdList;
			}
			set
			{
				this.m_DuctGeneralElementsIdList = value;
			}
		}

		public List<ElementId> FlexDuctGeneralElementsIdList
		{
			get
			{
				return this.m_FlexDuctGeneralElementsIdList;
			}
			set
			{
				this.m_FlexDuctGeneralElementsIdList = value;
			}
		}

		public List<ElementId> CableTrayGeneralElementsIdList
		{
			get
			{
				return this.m_CableTrayGeneralElementsIdList;
			}
			set
			{
				this.m_CableTrayGeneralElementsIdList = value;
			}
		}

		public List<ElementId> ConduitGeneralElementsIdList
		{
			get
			{
				return this.m_ConduitGeneralElementsIdList;
			}
			set
			{
				this.m_ConduitGeneralElementsIdList = value;
			}
		}

		public List<ElementId> WiresList
		{
			get
			{
				return this.m_WiresList;
			}
			set
			{
				this.m_WiresList = value;
			}
		}

		public List<ElementId> GeneralFamilyInstancesId
		{
			get
			{
				return this.m_GeneralFamilyInstancesId;
			}
			set
			{
				this.m_GeneralFamilyInstancesId = value;
			}
		}

		public List<ElementId> RoomsSpacesZonesAreasId
		{
			get
			{
				return this.m_RoomsSpacesZonesAreasId;
			}
			set
			{
				this.m_RoomsSpacesZonesAreasId = value;
			}
		}

		public List<ElementId> TagId
		{
			get
			{
				return this.m_TagId;
			}
			set
			{
				this.m_TagId = value;
			}
		}

		public List<ElementId> DoorsWindowsId
		{
			get
			{
				return this.m_DoorsWindowsId;
			}
			set
			{
				this.m_DoorsWindowsId = value;
			}
		}

		public List<FamilyInstance> AttachedToSurfaceOfFamilyInstances
		{
			get
			{
				return this.m_AttachedToSurfaceOfFamilyInstances;
			}
			set
			{
				this.m_AttachedToSurfaceOfFamilyInstances = value;
			}
		}

		public List<FamilyInstance> AttachedToWallOfFamilyInstances
		{
			get
			{
				return this.m_AttachedToWallOfFamilyInstances;
			}
			set
			{
				this.m_AttachedToWallOfFamilyInstances = value;
			}
		}

		public List<FamilyInstance> AttachedToCeilingOfFamilyInstances
		{
			get
			{
				return this.m_AttachedToCeilingOfFamilyInstances;
			}
			set
			{
				this.m_AttachedToCeilingOfFamilyInstances = value;
			}
		}

		public List<ElementId> DimensionsId
		{
			get
			{
				return this.m_DimensionsId;
			}
			set
			{
				this.m_DimensionsId = value;
			}
		}

		public ClassificationBatchConverterData(Autodesk.Revit.DB.Document doc)
		{
			this.m_Doc = doc;
			this.m_RailingGeneralElementsIdList = new List<ElementId>();
			this.m_StairsGeneralElementsIdList = new List<ElementId>();
			this.m_GroupGeneralElementsIdList = new List<ElementId>();
			this.m_RevitLinkInstanceGeneralElementsIdList = new List<ElementId>();
			this.m_OpeningGeneralElementsIdList = new List<ElementId>();
			this.m_TextNoteGeneralElementsIdList = new List<ElementId>();
			this.m_WallGeneralElementsIdList = new List<ElementId>();
			this.m_CeilingGeneralElementsIdList = new List<ElementId>();
			this.m_FloorGeneralElementsIdList = new List<ElementId>();
			this.m_PipeGeneralElementsIdList = new List<ElementId>();
			this.m_FlexPipeGeneralElementsIdList = new List<ElementId>();
			this.m_DuctGeneralElementsIdList = new List<ElementId>();
			this.m_FlexDuctGeneralElementsIdList = new List<ElementId>();
			this.m_CableTrayGeneralElementsIdList = new List<ElementId>();
			this.m_ConduitGeneralElementsIdList = new List<ElementId>();
			this.m_WiresList = new List<ElementId>();
			this.m_GeneralFamilyInstancesId = new List<ElementId>();
			this.m_RoomsSpacesZonesAreasId = new List<ElementId>();
			this.m_TagId = new List<ElementId>();
			this.m_DoorsWindowsId = new List<ElementId>();
			this.m_AttachedToSurfaceOfFamilyInstances = new List<FamilyInstance>();
			this.m_AttachedToWallOfFamilyInstances = new List<FamilyInstance>();
			this.m_AttachedToCeilingOfFamilyInstances = new List<FamilyInstance>();
			this.m_DimensionsId = new List<ElementId>();
		}

		~ClassificationBatchConverterData()
		{
			this.m_RailingGeneralElementsIdList.Clear();
			this.m_RailingGeneralElementsIdList = null;
			this.m_StairsGeneralElementsIdList.Clear();
			this.m_StairsGeneralElementsIdList = null;
			this.m_GroupGeneralElementsIdList.Clear();
			this.m_GroupGeneralElementsIdList = null;
			this.m_RevitLinkInstanceGeneralElementsIdList.Clear();
			this.m_RevitLinkInstanceGeneralElementsIdList = null;
			this.m_OpeningGeneralElementsIdList.Clear();
			this.m_OpeningGeneralElementsIdList = null;
			this.m_TextNoteGeneralElementsIdList.Clear();
			this.m_TextNoteGeneralElementsIdList = null;
			this.m_WallGeneralElementsIdList.Clear();
			this.m_WallGeneralElementsIdList = null;
			this.m_CeilingGeneralElementsIdList.Clear();
			this.m_CeilingGeneralElementsIdList = null;
			this.m_FloorGeneralElementsIdList.Clear();
			this.m_FloorGeneralElementsIdList = null;
			this.m_PipeGeneralElementsIdList.Clear();
			this.m_PipeGeneralElementsIdList = null;
			this.m_FlexPipeGeneralElementsIdList.Clear();
			this.m_FlexPipeGeneralElementsIdList = null;
			this.m_DuctGeneralElementsIdList.Clear();
			this.m_DuctGeneralElementsIdList = null;
			this.m_FlexDuctGeneralElementsIdList.Clear();
			this.m_FlexDuctGeneralElementsIdList = null;
			this.m_CableTrayGeneralElementsIdList.Clear();
			this.m_CableTrayGeneralElementsIdList = null;
			this.m_ConduitGeneralElementsIdList.Clear();
			this.m_ConduitGeneralElementsIdList = null;
			this.m_WiresList.Clear();
			this.m_WiresList = null;
			this.m_GeneralFamilyInstancesId.Clear();
			this.m_GeneralFamilyInstancesId = null;
			this.m_RoomsSpacesZonesAreasId.Clear();
			this.m_RoomsSpacesZonesAreasId = null;
			this.m_TagId.Clear();
			this.m_TagId = null;
			this.m_DoorsWindowsId.Clear();
			this.m_DoorsWindowsId = null;
			this.m_AttachedToSurfaceOfFamilyInstances.Clear();
			this.m_AttachedToSurfaceOfFamilyInstances = null;
			this.m_AttachedToWallOfFamilyInstances.Clear();
			this.m_AttachedToWallOfFamilyInstances = null;
			this.m_AttachedToCeilingOfFamilyInstances.Clear();
			this.m_AttachedToCeilingOfFamilyInstances = null;
			this.m_DimensionsId.Clear();
			this.m_DimensionsId = null;
		}

		public void GetGeneralElementsIdList(ref List<List<ElementId>> generalElementsIdList)
		{
			generalElementsIdList.Add(this.m_RailingGeneralElementsIdList);
			generalElementsIdList.Add(this.m_StairsGeneralElementsIdList);
			generalElementsIdList.Add(this.m_GroupGeneralElementsIdList);
			generalElementsIdList.Add(this.m_RevitLinkInstanceGeneralElementsIdList);
			generalElementsIdList.Add(this.m_OpeningGeneralElementsIdList);
			generalElementsIdList.Add(this.m_TextNoteGeneralElementsIdList);
			generalElementsIdList.Add(this.m_WallGeneralElementsIdList);
			generalElementsIdList.Add(this.m_CeilingGeneralElementsIdList);
			generalElementsIdList.Add(this.m_FloorGeneralElementsIdList);
			generalElementsIdList.Add(this.m_WiresList);
			generalElementsIdList.Add(this.m_RoomsSpacesZonesAreasId);
			generalElementsIdList.Add(this.m_TagId);
			generalElementsIdList.Add(this.m_DoorsWindowsId);
			generalElementsIdList.Add(this.m_DimensionsId);
			this.MepSystemGrouping(this.m_PipeGeneralElementsIdList, ref generalElementsIdList, ref this.m_GeneralFamilyInstancesId);
			this.MepSystemGrouping(this.m_FlexPipeGeneralElementsIdList, ref generalElementsIdList, ref this.m_GeneralFamilyInstancesId);
			this.MepSystemGrouping(this.m_DuctGeneralElementsIdList, ref generalElementsIdList, ref this.m_GeneralFamilyInstancesId);
			this.MepSystemGrouping(this.m_FlexDuctGeneralElementsIdList, ref generalElementsIdList, ref this.m_GeneralFamilyInstancesId);
			this.ElectricSystemGrouping(this.m_CableTrayGeneralElementsIdList, ref generalElementsIdList, ref this.m_GeneralFamilyInstancesId);
			this.ElectricSystemGrouping(this.m_ConduitGeneralElementsIdList, ref generalElementsIdList, ref this.m_GeneralFamilyInstancesId);
			generalElementsIdList.Add(this.m_GeneralFamilyInstancesId);
		}

		private void MepSystemGrouping(List<ElementId> MEPCurveIdList, ref List<List<ElementId>> generalElementsIdList, ref List<ElementId> instanceIdList)
		{
			Dictionary<ElementId, List<ElementId>> dictionary = new Dictionary<ElementId, List<ElementId>>();
			foreach (ElementId elementId in MEPCurveIdList)
			{
				Element element = this.m_Doc.GetElement(elementId);
				ElementId id;
				if (element.GetType() == typeof(Pipe))
				{
					id = (element as Pipe).MEPSystem.Id;
				}
				else if (element.GetType() == typeof(FlexPipe))
				{
					id = (element as FlexPipe).MEPSystem.Id;
				}
				else if (element.GetType() == typeof(Duct))
				{
					id = (element as Duct).MEPSystem.Id;
				}
				else
				{
					if (!(element.GetType() == typeof(FlexDuct)))
					{
						continue;
					}
					id = (element as FlexDuct).MEPSystem.Id;
				}
				if (!dictionary.ContainsKey(id))
				{
					dictionary.Add(id, new List<ElementId>
					{
						elementId
					});
				}
				else
				{
					dictionary[id].Add(elementId);
				}
			}
			for (int i = 0; i < instanceIdList.Count; i++)
			{
				ElementId elementId2 = instanceIdList[i];
				FamilyInstance familyInstance = this.m_Doc.GetElement(elementId2) as FamilyInstance;
				if (familyInstance.MEPModel != null && familyInstance.MEPModel.ConnectorManager != null)
				{
					ConnectorSetIterator connectorSetIterator = familyInstance.MEPModel.ConnectorManager.Connectors.ForwardIterator();
					try
					{
						while (connectorSetIterator.MoveNext())
						{
							object obj = connectorSetIterator.Current;
							Connector connector = obj as Connector;
							if (connector.MEPSystem != null)
							{
								ElementId id2 = connector.MEPSystem.Id;
								if (dictionary.ContainsKey(id2))
								{
									dictionary[id2].Add(elementId2);
									instanceIdList.RemoveAt(i);
									i--;
									break;
								}
							}
						}
					}
					catch (Exception)
					{
					}
				}
			}
			List<List<ElementId>> collection = dictionary.Values.ToList<List<ElementId>>();
			generalElementsIdList.AddRange(collection);
		}

		private void ElectricSystemGrouping(List<ElementId> MEPCurveIdList, ref List<List<ElementId>> generalElementsIdList, ref List<ElementId> instanceIdList)
		{
			new List<List<ElementId>>();
			for (int i = 0; i < MEPCurveIdList.Count; i++)
			{
				ElementId elementId = MEPCurveIdList[i];
				CableTrayConduitBase cableTrayConduitBase = this.m_Doc.GetElement(elementId) as CableTrayConduitBase;
				List<ElementId> list = new List<ElementId>();
				list.Add(elementId);
				ConnectorSetIterator connectorSetIterator = cableTrayConduitBase.ConnectorManager.Connectors.ForwardIterator();
				while (connectorSetIterator.MoveNext())
				{
					object obj = connectorSetIterator.Current;
					Connector otherConnector = obj as Connector;
					this.GetCableTrayConduitConnectElements(cableTrayConduitBase, otherConnector, ref MEPCurveIdList, ref instanceIdList, ref list);
				}
				generalElementsIdList.Add(list);
			}
		}

		private void GetCableTrayConduitConnectElements(CableTrayConduitBase cableTrayConduit, Connector otherConnector, ref List<ElementId> cableTrayConduitIdList, ref List<ElementId> instanceIdList, ref List<ElementId> group)
		{
			ConnectorSetIterator connectorSetIterator = cableTrayConduit.ConnectorManager.Connectors.ForwardIterator();
			while (connectorSetIterator.MoveNext())
			{
				object obj = connectorSetIterator.Current;
				Connector connector = obj as Connector;
				if (!Geometry.IsEqual(connector.Origin, otherConnector.Origin))
				{
					ConnectorSetIterator connectorSetIterator2 = connector.AllRefs.ForwardIterator();
					while (connectorSetIterator2.MoveNext())
					{
						object obj2 = connectorSetIterator2.Current;
						Connector connector2 = obj2 as Connector;
						Element owner = connector2.Owner;
						if (owner.Id.IntegerValue != cableTrayConduit.Id.IntegerValue)
						{
							group.Add(owner.Id);
							if (owner.GetType() == typeof(FamilyInstance))
							{
								this.FindAndDeleteElement(owner.Id, ref instanceIdList);
								this.GetFamilyInstanceConnectElements(owner as FamilyInstance, connector2, ref cableTrayConduitIdList, ref instanceIdList, ref group);
							}
							else
							{
								this.FindAndDeleteElement(owner.Id, ref cableTrayConduitIdList);
								this.GetCableTrayConduitConnectElements(owner as CableTrayConduitBase, connector2, ref cableTrayConduitIdList, ref instanceIdList, ref group);
							}
						}
					}
				}
			}
		}

		private void GetFamilyInstanceConnectElements(FamilyInstance instance, Connector otherConnector, ref List<ElementId> cableTrayConduitIdList, ref List<ElementId> instanceIdList, ref List<ElementId> group)
		{
			ConnectorSetIterator connectorSetIterator = instance.MEPModel.ConnectorManager.Connectors.ForwardIterator();
			while (connectorSetIterator.MoveNext())
			{
				object obj = connectorSetIterator.Current;
				Connector connector = obj as Connector;
				if (!Geometry.IsEqual(connector.Origin, otherConnector.Origin))
				{
					ConnectorSetIterator connectorSetIterator2 = connector.AllRefs.ForwardIterator();
					while (connectorSetIterator2.MoveNext())
					{
						object obj2 = connectorSetIterator2.Current;
						Connector connector2 = obj2 as Connector;
						Element owner = connector2.Owner;
						if (owner.Id.IntegerValue != instance.Id.IntegerValue)
						{
							group.Add(owner.Id);
							if (owner.GetType() == typeof(FamilyInstance))
							{
								this.FindAndDeleteElement(owner.Id, ref instanceIdList);
								this.GetFamilyInstanceConnectElements(owner as FamilyInstance, connector2, ref cableTrayConduitIdList, ref instanceIdList, ref group);
							}
							else
							{
								this.FindAndDeleteElement(owner.Id, ref cableTrayConduitIdList);
								this.GetCableTrayConduitConnectElements(owner as CableTrayConduitBase, connector2, ref cableTrayConduitIdList, ref instanceIdList, ref group);
							}
						}
					}
				}
			}
		}

		private void FindAndDeleteElement(ElementId id, ref List<ElementId> elementIdList)
		{
			int num = elementIdList.FindIndex((ElementId value) => value.IntegerValue == id.IntegerValue);
			if (num != -1)
			{
				elementIdList.RemoveAt(num);
			}
		}

		private Document m_Doc;

		private List<ElementId> m_RailingGeneralElementsIdList;

		private List<ElementId> m_StairsGeneralElementsIdList;

		private List<ElementId> m_GroupGeneralElementsIdList;

		private List<ElementId> m_RevitLinkInstanceGeneralElementsIdList;

		private List<ElementId> m_OpeningGeneralElementsIdList;

		private List<ElementId> m_TextNoteGeneralElementsIdList;

		private List<ElementId> m_WallGeneralElementsIdList;

		private List<ElementId> m_CeilingGeneralElementsIdList;

		private List<ElementId> m_FloorGeneralElementsIdList;

		private List<ElementId> m_PipeGeneralElementsIdList;

		private List<ElementId> m_FlexPipeGeneralElementsIdList;

		private List<ElementId> m_DuctGeneralElementsIdList;

		private List<ElementId> m_FlexDuctGeneralElementsIdList;

		private List<ElementId> m_CableTrayGeneralElementsIdList;

		private List<ElementId> m_ConduitGeneralElementsIdList;

		private List<ElementId> m_WiresList;

		private List<ElementId> m_GeneralFamilyInstancesId;

		private List<ElementId> m_RoomsSpacesZonesAreasId;

		private List<ElementId> m_TagId;

		private List<ElementId> m_DoorsWindowsId;

		private List<FamilyInstance> m_AttachedToSurfaceOfFamilyInstances;

		private List<FamilyInstance> m_AttachedToWallOfFamilyInstances;

		private List<FamilyInstance> m_AttachedToCeilingOfFamilyInstances;

		private List<ElementId> m_DimensionsId;
	}
}
